package com.ibs.hadoop.hbase.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.ibs.hadoop.hbase.bean.AttachBean;
import com.ibs.hadoop.hbase.bean.HbaseDataBean;
import com.ibs.hadoop.hbase.bean.QueryBean;
import com.ibs.hadoop.hbase.bean.UseComplaintBaseClass;
import com.ibs.hadoop.hbase.console.PropertiesEnumKey;
import com.ibs.hadoop.hbase.dao.base.HbaseProceDaoBase;
import com.ibs.hadoop.hbase.util.PropertiesUtils;

/**
 * 进行attach转换的操作
* 源文件名：AttachDaoImpl.java
* 文件版本：1.0.0
* 创建作者：liujun
* 创建日期：2016年10月31日
* 修改作者：liujun
* 修改日期：2016年10月31日
* 文件描述：liujun
* 版权所有：Copyright 2016 zjhz, Inc. All Rights Reserved.
*/
public class AttachDaoImpl extends HbaseProceDaoBase {

    /**
     * 日志 
    * @字段说明 log
    */
    private Logger log = Logger.getLogger(AttachDaoImpl.class);

    /**
     * 表名
    * @字段说明 PREFIX_TABLE_NAME
    */
    private static final String PREFIX_TABLE_NAME = "ATTACH_";

    /**
     *  获得实例对象,由于需要使用多线程加速方问，目录仅使用最原始的方式访问
    * @字段说明 INSTANCE
    */
    private static final AttachDaoImpl INSTANCE = new AttachDaoImpl();

    public static AttachDaoImpl getInstance() {
        return INSTANCE;
    }

    public List<UseComplaintBaseClass> query(QueryBean query) {
        log.info("AttachDaoImpl query param:" + query);

        String tableName = this.getTableName(query.getTableTime());

        List<UseComplaintBaseClass> result = null;

        List<HbaseDataBean> list = null;
        try {
            list = this.queryDef(tableName, query.getStartRowKey(), query.getEndRowKey());

            if (null != list && !list.isEmpty()) {
                result = new ArrayList<UseComplaintBaseClass>();

                for (HbaseDataBean hbaseDataBean : list) {
                    AttachBean bean = this.parseBean(hbaseDataBean);

                    if (bean != null) {
                        result.add(bean);
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            log.error("AttachDaoImpl query error:", e);
        }

        if (null != result && !result.isEmpty()) {
            log.info("AttachDaoImpl query param:" + query + ",result size :" + result.size());
        } else {
            log.info("AttachDaoImpl query param:" + query + ",result size is null     ! ");
        }

        return result;
    }

    private AttachBean parseBean(HbaseDataBean hbaseLine) {
        AttachBean attach = null;
        // 获取分隔符
        String spitFlag = PropertiesUtils.getInstance()
                .getValue(PropertiesEnumKey.HBASE_TABLE_COLUMN_SPIT_FLAG.getKey());

        String[] array = hbaseLine.getValue().split(spitFlag,-1);

        if (array != null && array.length == 54) {

            attach = new AttachBean();

            attach.setTac(array[0]);
            attach.setFrame(array[1]);
            attach.setTtime(array[2]);
            attach.setLastFrame(array[3]);
            attach.setEnbId(array[4]);
            attach.setCi(array[5]);
            attach.setImsi(array[6]);
            attach.setImei(array[7]);
            attach.setMTmsi(array[8]);
            attach.setMmecode(array[9]);
            attach.setNewMTmsi(array[10]);
            attach.setNewMmecode(array[11]);
            attach.setSuccess(array[12]);
            attach.setFailureCause(array[13]);
            attach.setResponseTime(array[14]);
            attach.setFailureMsg(array[15]);
            attach.setRecordFileIndex(array[16]);
            attach.setLastFileindex(array[17]);
            attach.setMobileType(array[18]);
            attach.setMobileBrand(array[19]);
            attach.setEnbIp(array[20]);
            attach.setMmeIp(array[21]);
            attach.setEnb(array[22]);
            attach.setMme(array[23]);
            attach.setAuthreqDelay(array[24]);
            attach.setAuthresDelay(array[25]);
            attach.setAttachCmp(array[26]);
            attach.setDelayCn(array[27]);
            attach.setDelayRadio(array[28]);
            attach.setLasttime(array[29]);
            attach.setWifiFlag(array[30]);
            attach.setProduceDate(array[31]);
            attach.setMpPrice(array[32]);
            attach.setOsImei(array[33]);
            attach.setAttachType(array[34]);
            attach.setAttResult(array[35]);
            attach.setEstablishCause(array[36]);
            attach.setMmeUeId(array[37]);
            attach.setAlgId(array[38]);
            attach.setRand(array[39]);
            attach.setUlTeid(array[40]);
            attach.setDlTeid(array[41]);
            attach.setEnbTransaddr(array[42]);
            attach.setUserIpValue(array[43]);
            attach.setCsfbCapability(array[44]);
            attach.setSGwIp(array[45]);
            attach.setTmsiCs(array[46]);
            attach.setIbsId(array[47]);
            attach.setZoneId(array[48]);
            attach.setErabReqDelay(array[49]);
            attach.setErabResDelay(array[50]);
            attach.setERabCause(array[51]);
            attach.setRatType(array[52]);
            attach.setVoicePrefered(array[53]);
        }

        return attach;
    }

    /**
     * 获取
    * 方法描述
    * @param time
    * @return
    * @创建日期 2016年10月31日
    */
    private String getTableName(String time) {
        String prefix = PropertiesUtils.getInstance().getValue(PropertiesEnumKey.HBASE_TABLE_PREFIX.getKey());
        return prefix + PREFIX_TABLE_NAME + time;
    }

    public static void main(String[] args) {
        AttachDaoImpl dao = new AttachDaoImpl();

        QueryBean bean = new QueryBean();
        bean.setImsi("460014418412330");
        bean.setStartTime("20161102");
        bean.setEndTime("20161102");

        List<UseComplaintBaseClass> list = dao.query(bean);
        
        System.out.println(list);
    }

}
